DAY33:What's a Perfect Power anyway?


Posted by birdbirdmurmur on 2023-08-14

題目連結

https://www.codewars.com/kata/54d4c8b08776e4ad92000835

解法

function isPP(n) {
    for (let i = 2; i <= n; i++) {
        let m = Math.round(Math.pow(n, 1 / i));

        if (m ** i === n) {
            return [m, i];
        }
    }
    return null;
}

筆記

先查了一下MDN看要怎麼開幾次根號
最後用了Math.pow(n, 1 / i)
開根號倒過來就是次方
使用Math.round四捨五入
反過來乘回去 就可以拿出來判斷了


#javascript #Codewars #Math.round #Math.pow







Related Posts

npm 和 npx 的差別

npm 和 npx 的差別

Python 程式設計函式的定義和呼叫入門教學

Python 程式設計函式的定義和呼叫入門教學

The introduction and difference between class component and function component in React

The introduction and difference between class component and function component in React


Comments